<template>
    <van-nav-bar title="京东注册" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-cell-group inset>
        <van-field v-model="telcode" center clearable label="短信验证码" placeholder="请输入短信验证码">
            <template #button>
                <van-button size="small" type="danger" @click="send" >发送验证码
                    <van-count-down v-show="!flag" :time="time" format=" ss 秒" :auto-start="false" @finish="finish" ref="cont"/>
                </van-button>
            </template>
        </van-field>
    </van-cell-group>
    <div style="margin: 16px;">
        <van-button  block type="primary" native-type="submit" @click="check">
            下一步
        </van-button>
    </div>
</template>

<script setup lang="ts">
    import { useRouter } from 'vue-router';
    import {dosendmsgcode,docheckcode} from "@/service/login"
    import {ref} from "vue"
 
    let router = useRouter()
    const onClickLeft = ()=>{
        router.push("/regist/step1")
    }
    let tel = ref<any>("")
    let telcode = ref<string>("")
    let time = ref<number>(1000*60)
    let flag = ref<boolean>(true)
    const cont = ref<any>("");
    const send = ()=>{
        tel.value = sessionStorage.getItem("tel")
        dosendmsgcode(tel.value).then((res:any)=>{
                if(res.code==="200"){
                    flag.value = false
                    cont.value.start()
                    console.log(res)
                }
            })
    }
    const finish = ()=>{
        flag.value = true
    }
    const check = ()=>{
        docheckcode(tel.value,telcode.value).then((res:any)=>{
               if(res.code==="200"){
                  router.push("/regist/step3")
               }
            })
    }
</script>

<style scoped>

</style>